<?php

namespace backend\modules\person\controllers;

use common\models\person\Hobby;
use Yii;
use common\components\Curd;
use common\models\base\SearchModel;
use common\models\person\Person;
use common\models\person\Cla;
use common\models\person\PersonHobbyMap;
use backend\modules\person\forms\PersonForm;
use backend\controllers\BaseController;

/**
 * Class NotifyAnnounceController
 * @package backend\modules\sys\controllers
 * @author jianyan74 <751393839@qq.com>
 */
class PersonController extends BaseController
{
    //behaviors方法 是行为过滤 执行方法时过滤 需要引入两个文件use yii\filters\AccessControl;和 use yii\filters\VerbFilter;
    /**
     * behaviors方法 详解 链接 https://www.cnblogs.com/lccjob/p/5057369.html
     */
    use Curd;

    /**
     * @var person
     */
    public $modelClass = Person::class;

    /**
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionIndex()
    {
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'partialMatchAttributes' => ['name'], // 模糊查询
            'defaultOrder' => [
                'id' => SORT_DESC
            ],
            'pageSize' => $this->pageSize
        ]);

        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);
        $dataProvider->query;
        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
            'claList'=>cla::getDropDown(),
        ]);
    }

    /**
     * 编辑/创建
     * @return mixed
     */
    public function actionEdit()
    {
        $request = Yii::$app->request;
        $id = $request->get('id', null);
        $model = $this->findModel($id);

        // 设置选中的爱好
        $hobbyMap = PersonHobbyMap::getHobbysByPersonId($id);
        $model->hobby_id = array_column($hobbyMap, 'hobby_id');
        $session = \Yii::$app->session;

        if($model->load($request->post())){
            if($session->get("code")!=$model->code){
                \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
                return ['code'=>400,'message'=>'验证码错误'];
                //return $this->message("验证码错误,请重新输入", $this->redirect(['edit']), 'error');
            }
        }
        if ($model->load($request->post()) && $model->save()) {
            PersonHobbyMap::addHobbys($model->id, $model->hobby_id);
            return $this->redirect(['index']);
        }
        return $this->render($this->action->id, [
            'claList'=>cla::getDropDown(),
            'getHobby'=>Hobby::getCheckHobby(),
            'model' => $model
        ]);
    }

    /**
     * 返回模型
     *
     * @param $id
     * @return \yii\db\ActiveRecord
     */
    protected function findModel($id)
    {
        /* @var $model \yii\db\ActiveRecord */
        if (empty($id) || empty(($model = Person::findOne($id)))) {
            $model = new PersonForm();
            return $model->loadDefaultValues();
        }
        return $model;
    }
    public function actionSendmail(){
        $session = \Yii::$app->session;
        $vals = \Yii::$app->request->post("val", '');
        $valss = \Yii::$app->request->post("vals", '');
        if($vals==''){
            $val = $valss;
        }else{
            $val = $vals;
        }
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
        $result = preg_match($pattern,$val);
        if(!$result){
            return ['code'=>400,'msg'=>'邮箱格式错误'];
        }
        $rand = rand(100000,999999);
        $session->set("code",$rand);
        //Yii::$app->services->mailer->send('卜鑫11', $val, 'nihao1', '');
        $mail=Yii::$app->mailer->compose();
        $mail->setTo($val);//要发送给那个人的邮箱   
        $mail->setSubject("邮件主题");//邮件主题   
        $mail->setTextBody('测试text');//发布纯文字文本   
        $mail->setHtmlBody("你的验证码是 $rand 请注意查收");//发送的消息内容
        return $mail->send();
    }
}